﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using CefSharp;

namespace DC
{
    public partial class Form_Main : Skin_DevExpress
    {
        public Form_Main()
        {
            InitializeComponent();
        }

        private void Form_Main_Load(object sender, EventArgs e)
        {
            Form_Login login = new Form_Login();
            login.ShowDialog();
            if (!login.success)
            {
                System.Environment.Exit(0);
                return;
            }
            UserID = login.UserID;
            checklimit();
            LoadData();
            ResetPatientInfo();
            loadDailySale();
        }

        private void checklimit()
        {
            string sql = @"select Limit from [User] where User_ID=" + UserID;
            skinButton5.Visible = DB.GetData(sql).ToString() == "1";
        }

        private void LoadData()
        {

            dateTimePicker1.Enabled = dateTimePicker2.Enabled = checkBox2.Checked = false;
            dateTimePicker2.Value = DateTime.Now;
            dateTimePicker1.Value = dateTimePicker2.Value.AddMonths(-1);

            LoadPatients();
            LoadUser();
            LoadNums();
            LoadToday();
        }

        Utils.Database DB = Utils.Global.GlobalDB;
        string UserID = string.Empty;

        private void LoadUser()
        {
            DataRow row = DB.GetDs("select User_ID,Real_Name from [User] where User_ID=" + UserID).Tables[0].DefaultView[0].Row;
            label28.Text =  row["Real_Name"].ToString() + "医生";
        }

        private void LoadPatients()
        {
            searchPatient();
        }

        private void patientList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (patientList.SelectedItems.Count == 0)
            {
                return;
            }
            DataRow currentPatient = DB.GetDs("select * from Patient where Patient_id='" + patientList.SelectedItems[0].Tag + "'").Tables[0].DefaultView[0].Row;
            label12.Text = currentPatient["Patient_id"].ToString();
            label13.Text = currentPatient["Patient_name"].ToString();
            label9.Text = currentPatient["Patient_location"].ToString();
            label11.Text = currentPatient["Patient_identity"].ToString();
            label15.Text = currentPatient["Patient_phone"].ToString();
            label24.Text = currentPatient["Patient_sex"].ToString();
            label17.Text = currentPatient["Patient_Birthday"].ToString().Substring(0, 10);
            label19.Text = currentPatient["Patient_debt"].ToString();
            label26.Text = currentPatient["Patient_last"].ToString();
        }
         
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            searchPatient();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            searchPatient();
        }
        private void searchPatient()
        {
            string sql = "select Patient_id,Patient_name,Patient_phone,Patient_Last from Patient where 1=1 ";
            if (textBox1.Text != string.Empty)
            {
                sql += "and Patient_name like'%" + textBox1.Text + "%'";
            }
            if (textBox2.Text != string.Empty)
            {
                sql += "and Patient_phone like'%" + textBox2.Text + "%'";
            }
            if (checkBox2.Checked)
            {
                sql += "and Patient_Last BETWEEN convert(datetime,'"+dateTimePicker1.Value.ToString("yyyy-MM-dd") +
                       "',120) AND convert(datetime,'"+ dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd") +"',120)";
            }
            //MessageBox.Show(sql);
            DataSet patientSet = DB.GetDs(sql);
            this.patientList.BeginUpdate();
            patientList.Items.Clear();
            for (int i = 0; i < patientSet.Tables[0].DefaultView.Count; i++)
            {
                DataRow row = patientSet.Tables[0].DefaultView[i].Row;
                ListViewItem item = new ListViewItem(row["Patient_name"].ToString());
                item.Tag = row["Patient_id"].ToString();
                item.SubItems.Add(row["Patient_phone"].ToString());
                item.SubItems.Add(row["Patient_Last"].ToString());
                this.patientList.Items.Add(item);
            }
            this.patientList.EndUpdate();
        }

        private void patientList_DoubleClick(object sender, EventArgs e)
        {
            if (patientList.SelectedItems.Count == 0)
            {
                return;
            }
            string patientID = patientList.SelectedItems[0].Tag.ToString();
            Views.Form_PatientInfo patientInfo = new Views.Form_PatientInfo();
            patientInfo.patientID = patientID;
            patientInfo.UserID = UserID;
            patientInfo.ShowDialog();
            LoadPatients();
            LoadNums();
            LoadToday();
            ResetPatientInfo();
            loadDailySale();
        }

        private void loadDailySale()
        {
            //加载今日营业额
            string sql = @"SELECT SUM(Fee_Real) FROM Fee WHERE User_ID=" + UserID + 
                          " AND CONVERT(VARCHAR,Fee_Time,120) LIKE '%" + DateTime.Now.ToString("yyyy-MM-dd") + "%'";
            string DailyCharge = DB.GetData(sql).ToString();
            if (DailyCharge == String.Empty)
            {
                label6.Text = "0";
            }
            else
            {
                label6.Text = DailyCharge;

            }

        }

        private void ResetPatientInfo()
        {
            label12.Text = label9.Text = label11.Text = label15.Text = label24.Text = label26.Text = label13.Text = label17.Text = label19.Text = String.Empty;
        }

        private void LoadToday()
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd");

            string sql = @"select Example_ID,Patient_Name,Patient_Phone,Example_Main,Example_Time from [User],Example,Patient
                        where Patient.Patient_ID = Example.Patient_ID and [User].User_ID = Example.User_ID and Convert(varchar, Example_Time, 120) like'%" + date + "%'" + "and Example.User_ID=" + UserID;
            DataSet todayExamples = DB.GetDs(sql);
            this.todayExample.BeginUpdate();
            todayExample.Items.Clear();
            for (int i = 0; i < todayExamples.Tables[0].DefaultView.Count; i++)
            {
                DataRow row = todayExamples.Tables[0].DefaultView[i].Row;
                ListViewItem item = new ListViewItem(row["Patient_name"].ToString());
                item.Tag = row["Example_ID"].ToString();
                item.SubItems.Add(row["Patient_phone"].ToString());
                item.SubItems.Add(row["Example_Main"].ToString());
                this.todayExample.Items.Add(item);
            }
            this.todayExample.EndUpdate();
            //-----------------------------------------------
            string sql2 = @"select Order_ID,Patient_Name,Patient_Phone,Order_Things,Order_Finish,Order_Time from Book,Patient
                        where Patient.Patient_ID = Book.Patient_ID and Convert(varchar, Order_Time, 120) like'%" + date + "%'" + "and Book.User_ID=" + UserID;
            DataSet todayOrders = DB.GetDs(sql2);
            this.todayOrder.BeginUpdate();
            todayOrder.Items.Clear();
            for (int i = 0; i < todayOrders.Tables[0].DefaultView.Count; i++)
            {
                DataRow row = todayOrders.Tables[0].DefaultView[i].Row;
                ListViewItem item = new ListViewItem(row["Patient_Name"].ToString());
                item.Tag = row["Order_ID"].ToString();
                item.SubItems.Add(row["Patient_Phone"].ToString());
                item.SubItems.Add(row["Order_Things"].ToString());
                item.SubItems.Add(row["Order_Finish"].ToString());
                this.todayOrder.Items.Add(item);
            }
            this.todayOrder.EndUpdate();
        }

        private void LoadNums()
        {
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string a = DB.GetData("select COUNT(*) FROM Example WHERE IsFirst=0 and Convert(varchar, Example_Time, 120) like'%" + date + "%'").ToString();
            label31.Text = a;
            string b = DB.GetData("select COUNT(*) FROM Example WHERE IsFirst=1 and Convert(varchar,Example_Time,120) like'%" + date + "%'").ToString();
            label32.Text = b;

            int c = Convert.ToInt32(DB.GetData("select count(*) from Book where Order_Finish='是' and Convert(varchar, Order_Time, 120) like'%" + date + "%' and User_ID=" + UserID).ToString()); //统计已到店
            int d = Convert.ToInt32(DB.GetData("select count(*) from Book where Convert(varchar, Order_Time, 120) like'%" + date + "%' and User_ID=" + UserID).ToString()); //统计今日预约总数
            label5.Text = c + " / " + d;
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            Views.Form_NewPatient newPatient = new Views.Form_NewPatient();
            newPatient.ShowDialog();
            LoadPatients();
        }
        private void skinButton2_Click(object sender, EventArgs e)
        {
            Views.Form_ProjectManagement ProjectManagenment = new Views.Form_ProjectManagement();
            ProjectManagenment.ShowDialog();
        }

        private void 预约ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (patientList.SelectedItems.Count == 0)
            {
                return;
            }
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            Views.Form_OrderList orderList = new Views.Form_OrderList();
            orderList.ShowDialog();
            LoadToday();
        }

        private void 已到店ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (todayOrder.SelectedItems.Count == 0)
            {
                return;
            }
            if (MessageBox.Show("确认已到店并就诊？", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DB.Execute("update Book set Order_Finish='是' where Order_ID=" + todayOrder.SelectedItems[0].Tag.ToString());
                LoadToday();
                LoadNums();
            }
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            Views.Form_MyFeeData form_MyFeeData = new Views.Form_MyFeeData();
            form_MyFeeData.UserID = UserID;
            form_MyFeeData.ShowDialog();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                loadDailySale();
            }
            else
            {
                label6.Text = "****";
            }
        }

        private void skinButton5_Click(object sender, EventArgs e)
        {
            Views.Form_TotalFeeData totalFeeData = new Views.Form_TotalFeeData();
            totalFeeData.ShowDialog();
        }

        private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            Cef.Shutdown();
        }

        private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            DB.Dispose();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Enabled = dateTimePicker2.Enabled = checkBox2.Checked;
            searchPatient();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            searchPatient();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            searchPatient();
        }
    }
}
